package es.deusto.tralala.rmi.client.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import es.deusto.tralala.rmi.client.controller.TralalaController;

public class HelloWindow extends JFrame implements ActionListener,
		ListSelectionListener, WindowListener {

	private static final long serialVersionUID = 1L;
	
	private TralalaController controller;
	
	private JLabel creado;
	private JButton bComenzar;
	private JPanel panelSur;
	private JPanel panelNorte;
	private JPanel panelCentro;
	private JProgressBar current;
	private JLabel img;
	private ImageIcon imageIcon;
	Thread runner;
	int num = 0;

	public HelloWindow() {

		super("VentanaHola");
		
		init();
	}
	
	public HelloWindow(TralalaController controller) {

		super("VentanaHola");
		
		init();
		
		this.controller = controller;
	}
	
	void init() {

		current = new JProgressBar(0, 2000);
		current.setValue(0);
		current.setStringPainted(true);

		creado = new JLabel("Created by SD Group 11");
		bComenzar = new JButton("Entrar");
		panelSur = new JPanel();
		panelNorte = new JPanel();
		panelCentro = new JPanel();

		img = new JLabel("");
		imageIcon = new ImageIcon("tortue_grande.jpg");
		img.setIcon(imageIcon);
		img.setVisible(true);
		bComenzar.setVisible(false);

		panelNorte.setLayout(new GridLayout(1, 3));
		panelNorte.add(creado);
		panelNorte.add(new JPanel());
		add("North", panelNorte);
		// panelNorte.setBackground(Color.WHITE);

		panelCentro.setLayout(new FlowLayout());
		panelCentro.add(img);
		add("Center", panelCentro);
		// panelCentro.setBackground(Color.WHITE);

		panelSur.setLayout(new FlowLayout());
		panelSur.add(current);
		panelSur.add(bComenzar);
		add("South", panelSur);
		// panelSur.setBackground(Color.WHITE);

		setTitle("Bienvenido a Tralala");
		setSize(700, 550);
		this.setResizable(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		// getContentPane().setBackground(Color.WHITE);

		this.addWindowListener(this);
		bComenzar.addActionListener(this);
		this.addWindowListener(this);
	}

	public void iterate() {
		while (num < 2000) {
			current.setValue(num);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
			num += 995;
		}
		bComenzar.setVisible(true);
	}

	public void valueChanged(ListSelectionEvent arg0) {

	}

	public void actionPerformed(ActionEvent e) {

		JButton b = (JButton) e.getSource();
		if (b == bComenzar) {
			this.setVisible(false);
			try {
				for (LookAndFeelInfo info : UIManager
						.getInstalledLookAndFeels()) {
					if ("Nimbus".equals(info.getName())) {
						UIManager.setLookAndFeel(info.getClassName());
						break;
					}
				}
			} catch (Exception e1) {

			}

			LoginWindow vi = new LoginWindow(controller);
			vi.setVisible(true);
			Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
			Dimension dimen = vi.getSize();
			vi.setLocation((pantalla.width - dimen.width) / 2,
					(pantalla.height - dimen.height) / 2);
		}
	}

	public static void main(String[] args) throws SQLException,
			ClassNotFoundException {

		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e1) {

		}
		HelloWindow vH = new HelloWindow();
		vH.setVisible(true);
		vH.iterate();
	}

	public void windowActivated(WindowEvent arg0) {

	}

	public void windowClosed(WindowEvent arg0) {

	}

	public void windowClosing(WindowEvent arg0) {
		System.exit(0);
	}

	public void windowDeactivated(WindowEvent arg0) {

	}

	public void windowDeiconified(WindowEvent arg0) {

	}

	public void windowIconified(WindowEvent arg0) {

	}

	public void windowOpened(WindowEvent arg0) {

	}

}
